/*
 * ttbqt - A teletekst browser
 * http://code.google.com/p/ttbqt
 * Copyright (C) 2011, Kevin van der Vlist
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Static tester for the TSSView shit
 * Font file: http://kevinvandervlist.nl/font.gif
 * Text page: http://kevinvandervlist.nl/page.dump
 */

#include "Teletext.h"
#include "TSSViewTeletext.h"

#include <QtDebug>

const QString TSSViewTeletext::TEXTNAME("TSSViewTeletext");

/**
 * \short Constructor of the base Teletext object. 
 * 
 * Constructor of the base Teletext object. 
 */

TSSViewTeletext::TSSViewTeletext(PageInfo *p) : Teletext() {
  channelname = TSSViewTeletext::TEXTNAME;

  pageinfo = p;
  qnam = new QNetworkAccessManager(this);
  QObject::connect(qnam, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadFinished(QNetworkReply*)));

  // Init the tssviewer
  QString fonturl("http://kevinvandervlist.nl/font.gif");
  tss = new TSSView(fonturl);
}

/**
 * \short Destructor of the base Teletext object. 
 * 
 * Destructor of the base Teletext object. 
 */

TSSViewTeletext::~TSSViewTeletext() {
  delete tss;
}

/**
 * Return a pixmap to the current page
 */

void TSSViewTeletext::getTextPage(int page, int subpage) {

  QString url;
  url.sprintf("http://kevinvandervlist.nl/page.dump");

  QNetworkRequest request;
  request.setUrl(QUrl(url));
  request.setRawHeader("User-Agent", USER_AGENT);
  reply = qnam->get(request);
}

/**
 * \short Slot function for a finished download
 * 
 * \param p Pointer to the QNetworkReply
 * 
 * Set the pixpage to the downloaded image. 
 * This function needs to be overridden if the download is not an image. 
 */

void TSSViewTeletext::downloadFinished(QNetworkReply* reply) {
  qDebug() << "TSSViewTeletext::downloadFinished() - url: " << reply->request().url();
  //qDebug() << "TSSViewTeletext::downloadFinished() - content: \n" << reply->readAll();
  // Downloaded text file:
  pixpage = tss->getPixmap(&(reply->readAll()));
  pageinfo->setTeletextPage(pixpage);
  reply->deleteLater();
}
